package sk.uniza.fri.cerviix.server.game;

public class Vector
{
	private float x, y;
	private float length;
	
	public Vector(float x, float y)
	{
		setByPoint(x, y);
	}
	
	public Vector(float degrees)
	{
		setByDegrees(degrees);
	}
	
	public void add(Vector vector)
	{
		x += vector.x;
		y += vector.y;
		
		calculateLength();
	}
	
	public void sub(Vector vector)
	{
		x -= vector.x;
		y -= vector.y;
		
		calculateLength();
	}
	
	private void setByPoint(float x, float y)
	{
		this.x = x;
		this.y = y;
		
		calculateLength();
	}
	
	private void setByDegrees(float degrees)
	{
		x = (float)Math.cos(degrees);
		y = (float)Math.sin(degrees);
		
		length = 1.0f;
	}
	
	private void calculateLength()
	{
		length = (float)Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
	}
	
	public void normalize()
	{
		calculateLength();
		
		x = x / length;
		y = y / length;
		
		length = 1.0f;
	}
	
	public float getLength()
	{
		return length;
	}
	
	public void change(float x, float y)
	{
		setByPoint(x, y);
	}
	
	public void change(float degrees)
	{
		setByDegrees(degrees);
	}
	
	public void scalar(float scalar)
	{
		x *= scalar;
		y *= scalar; 
	}
	
	public float getX()
	{
		return x;
	}
	
	public float getY()
	{
		return y;
	}
}
